package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.Condition;
import com.juneng.common.R;
import com.juneng.entity.User;
import com.juneng.mapper.UserMapper;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.Comment;
import com.juneng.mapper.CommentMapper;
import com.juneng.service.ICommentService;
import com.juneng.dto.CommentDto;
import com.juneng.dto.CommentQueryDto;
import com.juneng.vo.CommentVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import java.util.List;
/**
 * 社区评论表 服务实现类
 *
 * @author zds
 * @since 2021-07-28
 */
@Service
@AllArgsConstructor
public class CommentServiceImpl implements ICommentService {

    protected CommentMapper commentMapper;

    protected UserMapper userMapper;

    @Override
    public IPage<CommentVo> page(CommentQueryDto dto) {
        IPage<CommentVo> page = Condition.getPage(dto);
        return commentMapper.page(page,dto);
    }

    @Override
    public List<CommentVo> list(CommentQueryDto dto) {
        return commentMapper.getCommentList();
    }

    @Override
    public R<?> save(CommentDto dto) {
        //查询手机号是否存在
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, dto.getPhone()));
        if (user == null){
            return R.failed("手机号没有注册");
        }
        dto.setUserId(user.getUserId());
        int insert = commentMapper.insert(BeanCopyUtils.copy(dto, Comment.class));
        return R.success(insert) ;
    }

    @Override
    public Integer updateById(CommentDto dto) {
        return commentMapper.updateById(BeanCopyUtils.copy(dto,Comment.class));
    }

    @Override
    public Integer deleteLogic(Long id) {
        return commentMapper.deleteById(id);
    }

    @Override
    public CommentVo getById(Long id) {
        return commentMapper.getComment(id);
    }
}
